Add ClickHouse query layer for spans with feature flag support#2302
Open
Add ClickHouse query layer for spans with feature flag support#2302
Conversation
860578f to
d87876d
Compare
| @@ -0,0 +1 @@ | |||
| export const CLICKHOUSE_SPANS_READ_FLAG = 'clickhouse-spans-read' | |||
4ecda72 to
3c3930f
Compare
geclos
commented
Feb 16, 2026
packages/core/src/queries/clickhouse/spans/fetchConversation.ts
Outdated
Show resolved
Hide resolved
geclos
commented
Feb 16, 2026
packages/core/src/queries/clickhouse/spans/countMainTypesSince.ts
Outdated
Show resolved
Hide resolved
geclos
commented
Feb 16, 2026
geclos
commented
Feb 16, 2026
packages/core/src/queries/clickhouse/spans/findByDocumentLogUuid.ts
Outdated
Show resolved
Hide resolved
geclos
commented
Feb 16, 2026
packages/core/src/queries/clickhouse/spans/findBySpanAndTraceIds.ts
Outdated
Show resolved
Hide resolved
geclos
commented
Feb 16, 2026
geclos
commented
Feb 16, 2026
packages/core/src/queries/clickhouse/spans/getAnnotationsProgress.ts
Outdated
Show resolved
Hide resolved
geclos
commented
Feb 16, 2026
geclos
commented
Feb 16, 2026
packages/core/src/queries/clickhouse/spans/getSpansWithoutIssues.ts
Outdated
Show resolved
Hide resolved
geclos
commented
Feb 16, 2026
| AND document_uuid = {documentUuid: UUID} | ||
| AND commit_id IN ({commitIds: Array(UInt64)}) | ||
| AND evaluated_span_id IS NOT NULL | ||
| AND id IN ({evaluationResultIds: Array(UInt64)}) |
Collaborator
Author
There was a problem hiding this comment.
if we are passing ids as a filter why do we need all the other filters in this query
geclos
commented
Feb 16, 2026
packages/core/src/queries/clickhouse/spans/getSpansWithoutIssues.ts
Outdated
Show resolved
Hide resolved
geclos
commented
Feb 16, 2026
packages/core/src/queries/clickhouse/spans/getSpansWithoutIssues.ts
Outdated
Show resolved
Hide resolved
geclos
commented
Feb 16, 2026
packages/core/src/queries/clickhouse/spans/getUsageOverviewCounts.ts
Outdated
Show resolved
Hide resolved
geclos
commented
Feb 16, 2026
packages/core/src/queries/clickhouse/spans/hasProductionTraces.ts
Outdated
Show resolved
Hide resolved
geclos
commented
Feb 16, 2026
packages/core/src/queries/clickhouse/spans/hasProductionTraces.ts
Outdated
Show resolved
Hide resolved
geclos
commented
Feb 16, 2026
packages/core/src/queries/clickhouse/spans/hasProductionTraces.ts
Outdated
Show resolved
Hide resolved
geclos
commented
Feb 16, 2026
geclos
commented
Feb 16, 2026
geclos
commented
Feb 16, 2026
geclos
commented
Feb 16, 2026
Collaborator
Author
|
@claude fix all comments |
f7e5d02 to
874c8ee
Compare
Add ClickHouse query functions for all SpansRepository read methods,
gated behind the existing 'clickhouse-spans-read' feature flag. When
the flag is enabled for a workspace, reads are served from ClickHouse
instead of PostgreSQL.
New files:
- queries/clickhouse/spans/*.ts: ClickHouse query implementations
for get, list, findLimited, findByDocumentLogUuid, etc.
- queries/clickhouse/spans/toSpan.ts: SpanRow→Span conversion utility
- repositories/createSpansRepository.ts: factory that resolves the
feature flag and instantiates SpansRepository with useClickHouse
Modified:
- SpansRepository: accepts optional { useClickHouse } option; each
public method delegates to the corresponding ClickHouse query when
the flag is set, otherwise falls back to existing PostgreSQL logic.
https://claude.ai/code/session_013nmboM3k7H1FUgYHQpoJQj
Instead of requiring a factory function at every call site,
SpansRepository now auto-detects whether to use ClickHouse by lazily
checking the feature flag on the first method call and caching the
result for the instance lifetime.
- Default behavior (no options): lazily checks the feature flag,
so toggling it per-workspace immediately routes all reads
- Explicit override: pass { useClickHouse: true/false } to force
a specific backend (used in processBulk.ts write path)
- Removed createSpansRepository factory since it's no longer needed
- Pinned processBulk.ts to PostgreSQL since it reads within a
write transaction where data hasn't been committed to ClickHouse
https://claude.ai/code/session_013nmboM3k7H1FUgYHQpoJQj
- Remove unnecessary type filter from fetchConversation query - Count distinct traces instead of spans in countMainTypesSince - Rename functions for clarity: - getSpan -> findSpan - listSpans -> findSpans - findBySpanAndTraceIds -> findBySpanAndTraceIdPairs - getSpansCountForAnnotationsProgress -> countSpansForAnnotations - Simplify hasProductionTraces by removing unnecessary filters and using equality check - Move utility functions (orUndefined, parseClickHouseDate) to lib/typeConversions.ts - Fix getSpansWithoutIssues to use commit UUIDs instead of IDs in ClickHouse queries - Remove redundant subquery in PostgreSQL getSpansWithoutIssues - When filtering by evaluation result IDs, remove unnecessary document_uuid and commit filters Co-authored-by: Gerard <geclos@users.noreply.github.com>
Metadata JSON payloads are now gzip-compressed before being written to disk/S3, reducing storage costs. Decompression on read is transparent to callers and backward-compatible with existing uncompressed data (detected via gzip magic bytes). https://claude.ai/code/session_01VC6xgLtYYKVkhXenuyYAGT Co-authored-by: Claude <noreply@anthropic.com>
Extract duplicated logic from getByDocument and getSpansWithoutIssues into reusable helpers: buildExperimentExclusionCondition for the optimization experiment filtering, and paginateSpanRows for the limit+1 cursor pagination pattern. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Wrap span query exports with scopedQuery for consistent workspace-scoped query definitions and shared error unwrapping semantics.
fe1739e to
4c8abe5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces a ClickHouse query layer for spans data, allowing the application to read span data from ClickHouse instead of the primary database. The implementation includes a feature flag (
clickhouse-spans-read) to control which workspaces use the new query layer.Key Changes
New ClickHouse Query Modules: Added comprehensive query functions for spans in ClickHouse:
get.ts- Retrieve a single span by ID and trace IDlist.ts- List all spans for a tracefindLimited.ts- Paginated span queries with cursor support and default time window handlingfindByDocumentLogUuid.ts- Query spans by document log UUID with various filtering optionsfindBySpanAndTraceIds.ts- Batch queries for spans and parent-child relationshipsfindByEvaluationResults.ts- Query spans referenced by evaluation resultscountByProjectAndSource.ts- Count spans by sourcetoSpan.ts- Convert ClickHouse row format to Span domain objectsgetByDocumentLogUuidAndSpanId.ts- Composite query helperSpansRepository Enhancement: Modified
SpansRepositoryto support dual-mode operation:useClickHouseto enable ClickHouse queriesFeature Flag Integration: Created
createSpansRepository.tsfactory function that:clickhouse-spans-readfeature flag for the workspaceSpansRepositorywith appropriate backend selectionImplementation Details
https://claude.ai/code/session_013nmboM3k7H1FUgYHQpoJQj